from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils import timezone

from apps.user.models import User


class Category(models.Model):
    """
    文章分类
    """
    name = models.CharField(verbose_name='文章类别', max_length=20)

    class Meta:
        db_table = 'tb_category'
        verbose_name = '文章类别'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class News(models.Model):
    """自定义新闻模型类"""
    title = models.CharField(verbose_name='标题', max_length=100)
    content = models.TextField(verbose_name='正文', default='')
    create_time = models.DateTimeField(verbose_name='创建时间', default=timezone.now)
    modify_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    click_nums = models.IntegerField(verbose_name='点击量', default=0)
    comment_num = models.IntegerField(verbose_name='评论量', default=0)
    status = models.IntegerField(verbose_name='是否通过', default=1)  # -1未通过 1审核中 0通过
    reason = models.CharField(verbose_name='未通过原因', max_length=256, null=True, blank=True, )
    award = models.IntegerField(verbose_name="赏赐飞吻数", default=0)
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='news', verbose_name='用户')

    category = models.ForeignKey(Category, verbose_name='文章类别')

    # tag = models.ManyToManyField(Tag, verbose_name='文章标签')

    class Meta:
        db_table = 'tb_news'
        verbose_name = '论坛'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class Collection(models.Model):
    """收藏"""
    user = models.IntegerField(verbose_name='用户的id')
    news = models.IntegerField(verbose_name='新闻的id')
    create_time = models.DateTimeField(verbose_name='收藏时间', default=timezone.now)
    is_deleted = models.BooleanField(default=False, verbose_name='逻辑删除')

    class Meta:
        db_table = 'tb_collection'
        verbose_name = '论坛'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.news


class Comment(models.Model):
    """帖子评论表"""
    content = models.CharField(verbose_name="评论的内容", max_length=255)
    belong_user = models.ForeignKey(User, related_name='user', verbose_name='用户')
    news = models.ForeignKey(News, related_name="article", verbose_name='新闻的id')
    to_user = models.ForeignKey("self", verbose_name='父类的id', null=True, related_name="to_users")
    create_time = models.DateTimeField(verbose_name='收藏时间', default=timezone.now)
    like_num = models.IntegerField(verbose_name="点赞", default=0)

    class Meta:
        db_table = 'tb_comment'
        verbose_name = '评论'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.belong_user.username + ':' + self.content
